﻿using CommunityToolkit.Mvvm.Messaging;
using PLCNET5_11_9.ViewModel;
using S7.Net.Types;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace PLCNET5_11_9
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// WrapPanel -- 和 Srap不一样，他是从头开始的，S是中间开始的 。。
    /// UniformGrid 默认就是2ROW 2COL。。。
    /// 比如写成 Colums = 2 Rows = 3 -- 就变成 3行2列了..
    /// UniforGrid Colums= 3 -- 这样3个按钮的间隙都是一样的  。。
    public partial class MainWindow : Window
    {
        MainViewModel viewModel = new MainViewModel();        
        
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = viewModel;            
            
            WeakReferenceMessenger.Default.Register<string, string>(this, "ScrollEnd", ScrollEnd);
            WeakReferenceMessenger.Default.Register<string, string>(this, "SWaitchXZ", SWaitchXZ);

            //这里必须是1，因为0 是LANGUAGE 2是我自己定义的..
            viewModel.resources = Application.Current.Resources.MergedDictionaries[1];
            viewModel.InitializeAsync();
        }

        ~MainWindow()
        {
            WeakReferenceMessenger.Default.UnregisterAll(this);
        }

        public void ChangeLanguage(string languageCode)
        {
            //这里和APP里面去对应就OK了... 我懂了,[1] 表示APP合并的XAML，使用数组就可以对应起来了...
            ResourceDictionary newLanguageDict = new ResourceDictionary();
            string resourcePath = $"Resources.{languageCode}.xaml";
            try
            {
                newLanguageDict.Source = new Uri(resourcePath, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries[1] = newLanguageDict;
                //这里要更新一下...
                viewModel.resources = Application.Current.Resources.MergedDictionaries[1];
                Application.Current.Resources["Language"] = languageCode;
                

            }
            catch (Exception ex)
            {
                // Handle exception
            }
        }

        //这里不要使用等待了..(主线程退出有问题..)

        //这里调用就是跳转到最前面...
        private void ScrollEnd(object recipient, string message)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                //获取ListView的最后一项
                if (myListBox.Items.Count > 3)
                {
                    var lastItem = myListBox.Items[myListBox.Items.Count - 1];
                    // 滚动到最后一项
                    myListBox.ScrollIntoView(lastItem);
                }
            });
        }

        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private bool currentYuyan = true;
        private void Button_Click_Swaich(object sender, RoutedEventArgs e)
        {
            currentYuyan = !currentYuyan;
            if (currentYuyan)
            {
                ChangeLanguage("ch");
                BtnSwaichButton.Content = "中";

            }
            else
            {
                ChangeLanguage("en");
                BtnSwaichButton.Content = "英";
            }
        }


        private void SWaitchXZ(object recipient, string message)
        {
            
            if (message == "ShowAllPlcDian")
            {

                //q0.0
                _ = viewModel.alldian_bool[0];//PC/按钮选择
                _ = viewModel.alldian_bool[1];//空
                _ = viewModel.alldian_bool[2];//空
                _ = viewModel.alldian_bool[3];//中频机运行/停止
                _ = viewModel.alldian_bool[4];//中频机三相指示
                _ = viewModel.alldian_bool[5];//中频机输出单相
                myButtonG8.myButtonModel.Data = viewModel.alldian_bool[6];//中频机输出合/分指示
                _ = viewModel.alldian_bool[7];//中频机磁力合/分指示

                //q1.0 myButton7_4 中变
                _ = viewModel.alldian_bool[8];//工频机运行合/分指示
                _ = viewModel.alldian_bool[9];//
                _ = viewModel.alldian_bool[10];//空
                _ = viewModel.alldian_bool[11];//空
                _ = viewModel.alldian_bool[12];//空
                _ = viewModel.alldian_bool[13];//空
                _ = viewModel.alldian_bool[14];//空
                _ = viewModel.alldian_bool[15];//空                

                //q8.0
                _ = viewModel.alldian_bool[16];//工频机输出三相
                _ = viewModel.alldian_bool[17];//工频机输出单相
                myButtonG6.myButtonModel.Data = viewModel.alldian_bool[18];//工频机输出合/分
                myButtonD4.myButtonModel.Data = viewModel.alldian_bool[19];//工频机磁力 合/分
                myButton3_2.myButtonModel.Data = viewModel.alldian_bool[20];//调压器输入 合/分
                myButton7_1.myButtonModel.Data = viewModel.alldian_bool[21];//调压器输出 合/分
                _ = viewModel.alldian_bool[22];//调压器
                _ = viewModel.alldian_bool[23];//调压器
                
                //q9.0
                _ = viewModel.alldian_bool[24];//调压器
                _ = viewModel.alldian_bool[25];//调压器
                _ = viewModel.alldian_bool[26];//调压器
                _ = viewModel.alldian_bool[27];//中频机实验
                _ = viewModel.alldian_bool[28];//工频机实验
                _ = viewModel.alldian_bool[29];//调压器实验
                _ = viewModel.alldian_bool[30];//支持变合/分
                _ = viewModel.alldian_bool[31];//电容AC合/分

                //q12.0
                _ = viewModel.alldian_bool[32];//电容B合/分
                _ = viewModel.alldian_bool[33];//电抗补偿合/分
                _ = viewModel.alldian_bool[34];//电抗A增
                _ = viewModel.alldian_bool[35];//电抗A减
                _ = viewModel.alldian_bool[36];//电抗B增
                _ = viewModel.alldian_bool[37];//电抗B减
                _ = viewModel.alldian_bool[38];//电抗C增
                _ = viewModel.alldian_bool[39];//电抗C减

                //q13.0 myButtonG3
                _ = viewModel.alldian_bool[40]; //中变合/分                
                myButtonGW1A.myButtonModel.Data = viewModel.alldian_bool[41];  //工位1合/分
                _ = viewModel.alldian_bool[42]; //工位2合/分
                viewModel.mainModel.Sgw3 = viewModel.alldian_bool[43];//工位3合/分
                _ = viewModel.alldian_bool[44];
                _ = viewModel.alldian_bool[45];
                _ = viewModel.alldian_bool[46];
                _ = viewModel.alldian_bool[47];

                //q16.0
                _ = viewModel.alldian_bool[48];
                _ = viewModel.alldian_bool[49];
                _ = viewModel.alldian_bool[50];
                _ = viewModel.alldian_bool[51];
                _ = viewModel.alldian_bool[52];
                _ = viewModel.alldian_bool[53];
                _ = viewModel.alldian_bool[54];
                _ = viewModel.alldian_bool[55];

                //q17.0
                _ = viewModel.alldian_bool[56];
                _ = viewModel.alldian_bool[57];
                _ = viewModel.alldian_bool[58];
                _ = viewModel.alldian_bool[59];
                _ = viewModel.alldian_bool[60];
                _ = viewModel.alldian_bool[61];
                _ = viewModel.alldian_bool[62];
                _ = viewModel.alldian_bool[63];
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            viewModel.MainClose();
        }

        private void MyKeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.SystemKey ==  Key.F10)
            {
                // 你的逻辑代码
                SetOneWindow processWindow = new SetOneWindow();
                processWindow.ShowDialog();
            }
        }
    }
}
